package com.example.servicemodule

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Binder
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity() {
    private val TAG = "MainActivity"
    private lateinit var button1:Button
    private val connection = object: ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
//            TODO("Not yet implemented")
            Log.e(TAG, "onServiceConnected: " )
        }

        override fun onServiceDisconnected(name: ComponentName?) {
//            TODO("Not yet implemented")
            Log.e(TAG, "onServiceDisconnected: " )
        }

    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        this.button1=findViewById(R.id.button1)
    }

    fun startMyService(view: View) {

        startService(Intent(this,MyLocalService::class.java))
    }
    fun stopMyService(view: View) {
        stopService(Intent(this,MyLocalService::class.java))
    }
    fun bindMyService(view: View) {
        bindService(Intent(this,MyLocalService::class.java),this.connection, BIND_AUTO_CREATE)
    }
    fun unbindMyService(view: View) {
        unbindService(connection)
        Log.e(TAG, "unbindMyService: " )
    }

    /**
     * 在Activity退出之前调用
     */
    override fun onDestroy() {
        super.onDestroy()
        unbindService(connection)
    }
}